# -*- coding: utf-8 -*-
"""
Created on Wed Nov 11 14:38:53 2020

@author: Administrator
"""
def getText():
    txt=open("hamlet.txt","r").read()
    txt=txt.lower()
    for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_{|}~‘’':
        txt=txt.replace(ch," ")
    return txt
fo=getText()
fo=fo.split()
dic={}
for i in fo:
    dic[i]=dic.get(i,0)+1
dic=list(dic.items())
dic.sort(key=lambda x:x[1],reverse=True)
print("hamlet单词统计结果如下：")
for i in range(10):
    print("单词：{0:<10}次数：{1}".format(dic[i][0],dic[i][1]))

